#!/bin/bash


osscmd="/workspace/share/ota/script/osscmd"
genjson="/workspace/share/ota/script/gen_json.py"
osshost="oss-cn-beijing.aliyuncs.com"
$osscmd config --id='aDo4ZtbecPs4GrAh' --key='HO0xFyI8zRdGFV5KrH5oJgpG8qG5mY'

file_is_uploaded() {
  etag=`$osscmd meta oss://inspero-rom-update-ossbucket/rom-$version/$filename |grep "etag" | awk -F'"' '{printf("%s",$2)}'`
  if [ "x$etag" = "x" ]; then
     return 0
  fi
  mkdir -p /tmp/uromsync
  cd /tmp/uromsync
  split -b 10485760 -d $file
  cd - >/dev/null 2>&1
  allmd=""
  for i in `ls /tmp/uromsync/x*`
  do
     xmd=`md5sum $i | awk '{printf("%s",$1)}'| tr '[:lower:]' '[:upper:]'`
     allmd=$allmd$xmd
   done
   localetag=`echo -n $allmd | md5sum |awk '{printf("%s",$1)}' | tr '[:lower:]' '[:upper:]'`"-"`ls /tmp/uromsync/x* | wc -l`
   rm -f /tmp/uromsync/*
   if [ $localetag = $etag ]; then
      return 1
   fi
   return 0
}

init_uploadid() {
  uploadid=`$osscmd init oss://inspero-rom-update-ossbucket/rom-$version/$filename --host=$osshost 2>/dev/null | grep "Upload"`
  count=`echo $uploadid | grep "Upload" | wc -l`
  if [ $count -ne 1 ]; then
    echo -e "\e[1;31m init oss error\e[0m" >&2; exit 1
  fi
  uploadid=`echo $uploadid | awk -F': ' '{print $2}'`
}

uploadfile() {
  retry=0
  filemd5=`md5sum $file | awk '{print $1}'`
  echo $filemd5
  while [ $retry -lt 3 ];
  do
    if [ $retry -gt 0 ]; then
      echo -e "\e[1;31m retry times: $retry\e[0m"
    fi
    $osscmd mp $file oss://inspero-rom-update-ossbucket/rom-$version/$filename --upload_id=$uploadid --thread_num=5 --max_part_num=100 --host=$osshost --headers="x-oss-md5sum:$filemd5"
    if [ $? -eq 0 ]; then
       return 0
    fi
    retry=$((retry+1))
  done
  return 1
}


version=$1
dir=$2

#判断dir是否存在
if [ "x$dir" = "x" ]; then
  echo -e "\e[1;31m ./syncrom <version> <rom_update_dir>\e[0m" >&2; exit 1
fi
if [ ! -d $dir ]; then
   echo -e "\e[1;31m $dir is not a regular directory\e[0m" >&2; exit 1
fi

#判断version是否符合要求
re='^[0-9]+(.[0-9]+)*$'
if ! [[ $version =~ $re ]] ; then
   echo -e "\e[1;31m error: version($version) Not a number\e[0m" >&2; exit 1
fi

cd $dir
dir=`pwd`
checkversion=`echo $dir | awk -F'/' '{print $NF}'`
cd - >/dev/null 2>&1

if [ "x$version" != "x$checkversion" ]; then
   echo -e "\e[1;31m $dir is not match version($version)\e[0m" >&2; exit 1
fi

if [ ! -f "$dir/description.txt" ]; then
  echo -e "\e[1;31m description.txt is not exist\e[0m" >&2; exit 1
fi

if [ ! -f "$dir/$version.zip" ]; then
  echo -e "\e[1;32m full rom file(${version}.zip) is not exist\e[0m" >&2
fi

filenames=(`ls $dir/*${version}.zip` $dir/description.txt)
echo -e "\e[1;34m upload file:"${filenames[*]}"\e[0m"
for file in ${filenames[*]}
do
  filename=`echo $file | awk -F'/' '{print $NF}'`
  file_is_uploaded
  if [ $? -eq 1 ]; then
    echo -e "\e[1;34m upload file:"${filename}" is exist\e[0m"
    continue
  fi
  init_uploadid
  echo -e "\e[1;34m start upload $filename , and uploadid: $uploadid\e[0m"
  uploadfile
  if [ $? -ne 0 ]; then
      echo -e "\e[1;31m occurred error , exit\e[0m" >&2; exit 1
  fi
  echo -e "\e[1;34m upload file:"${filename}" has uploaded\e[0m"
done

python $genjson $version $dir
if [ $? -eq 0 ]; then
  echo -e "\e[1;34mota_info.json has generated, please check and exec sync_ota_info <ota_info.json file>\e[0m"
else
  echo -e "\e[1;31mta_info.json generate failed , check it\e[0m" >&2; exit 1
fi
